package com.cn.onetrip.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;

import com.cn.onetrip.adapter.MoreGridAdapter;
import com.cn.onetrip.untility.NetworkManager;
import com.cn.onetrip.untility.SysApplication;
import com.cn.onetrip.activity.R;

public class MoreGridActivity extends Activity implements OnItemClickListener {

	private String feedBackUrl = "http://tm.1trip.com/feedback";
	private String stateUrl = "http://tm.1trip.com/state";
	private ArrayList<String> mNameList = new ArrayList<String>();
	private ArrayList<Drawable> mDrawableList = new ArrayList<Drawable>();
	private GridView gridView;
	private MoreGridAdapter adapter;
	private Button quit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Window w = getWindow();
		// view = w.getDecorView();
		if (SysApplication.routesList.size() == 0) {
			finish();
			android.os.Process.killProcess(android.os.Process.myPid());
		}
		setContentView(R.layout.activity_more_grid);

		InitView();

		AddContentItems();
		adapter = new MoreGridAdapter(getApplicationContext(), mNameList,
				mDrawableList);
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(this);

		quit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
				overridePendingTransition(R.anim.push_left_translate,
						R.anim.pull_right_translate);
			}
		});
	}

	private void InitView() {
		gridView = (GridView) findViewById(R.id.grid);
		quit = (Button) findViewById(R.id.quit);
	}

	private void AddContentItems() {
		mNameList.add(getResources().getString(R.string.more_suggest));
		mNameList.add(getResources().getString(R.string.more_user_protocol));
		// mNameList.add(getResources().getString(R.string.more_download_offline));

		mDrawableList.add(getResources().getDrawable(
				R.drawable.more_suggests_selector));
		mDrawableList.add(getResources().getDrawable(
				R.drawable.more_user_protocol_selector));
		// mDrawableList.add(getResources().getDrawable(
		// R.drawable.more_download_offline_selector));

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent(MoreGridActivity.this, MoreWebActivity.class);
		Bundle bundle = new Bundle();
		switch (arg2) {
		case 0:
			if (!NetworkManager.CheckNetworkState(getApplicationContext())) {
				Toast.makeText(getApplicationContext(),
						getResources().getString(R.string.network_fail),
						Toast.LENGTH_LONG).show();
				return;
			}
			bundle.putString("webName", "feedback");
			bundle.putString("webUrl", feedBackUrl);
			intent.putExtras(bundle);
			startActivity(intent);
			overridePendingTransition(R.anim.push_right_translate,
					R.anim.pull_left_translate);
			break;
		case 1:
			bundle.putString("webName", "state");
			bundle.putString("webUrl", stateUrl);

			intent.putExtras(bundle);
			startActivity(intent);
			overridePendingTransition(R.anim.push_right_translate,
					R.anim.pull_left_translate);
			break;
		case 2:
			// bundle.putString("webName", "App");
			// bundle.putString("webUrl", feedBackUrl);
			// intent.putExtras(bundle);
			// startActivity(intent);
			// overridePendingTransition(R.anim.push_right_translate,
			// R.anim.pull_left_translate);
			break;

		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			overridePendingTransition(R.anim.push_left_translate,
					R.anim.pull_right_translate);
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mNameList.clear();
		mDrawableList.clear();
	}
}
